﻿function UI_Date() { }

// Get last visible date month in the calender popup
UI_Date.getCalenderEndDate = function (intMonths) {
    var dtToday = new Date();
    if (arguments.length == 2) {
        if (arguments[1] != "") {
            dtToday = new Date(arguments[1].substr(6, 4), Number(arguments[1].substr(3, 2)) - 1, "01");
        }
    }

    // See if we got anything
    var dtTD = dtToday.getDate();

    // Retrieve the Date pieces
    var strMonth = dtToday.getMonth() + new Number(intMonths);
    var strDay = 1; //dtToday.getDate();
    var strYear = dtToday.getFullYear();

    // Retrieve the Time pieces
    var strHour = dtToday.getHours();
    var strMinutes = dtToday.getMinutes();
    var strSeconds = dtToday.getSeconds();
    var dtNew = new Date(strYear, strMonth, strDay, strHour, strMinutes, strSeconds);

    var dtNew1 = UI_Date.addDays(dtNew, -1);

    var dtNM = dtNew1.getMonth() + 1;
    var dtND = dtNew1.getDate();
    var dtNY = dtNew1.getFullYear();

    if (Number(dtTD) > UI_Date.getDaysInMonth(dtNM, dtNY)) {
        dtND = UI_Date.getDaysInMonth(dtNM, dtNY);
    }

    if (Number(dtND) < 10) { dtND = "0" + dtND; }
    if (Number(dtNM) < 10) { dtNM = "0" + dtNM; }

    // Create a new Date Object
    return dtND + "/" + dtNM + "/" + dtNY;
}

// GET NUMBER OF DAYS IN MONTH
UI_Date.getDaysInMonth = function(month, year) {
    var days;
    if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 ||
		month == 10 || month == 12) days = 31;
    else if (month == 4 || month == 6 || month == 9 || month == 11) days = 30;
    else if (month == 2) {
        if (isLeapYear(year)) {
            days = 29;
        }
        else {
            days = 28;
        }
    }
    return (days);
}

// add days to a date
UI_Date.addDays = function(dtDate, intDays) {
    return new Date(dtDate.getTime() + Number(intDays) * 24 * 60 * 60 * 1000);
}
